{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pandas的分层索引MultiIndex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为什么要学习分层索引MultiIndex？\n",
    "\n",
    "* 分层索引：在一个轴向上拥有多个索引层级，可以表达更高维度数据的形式;\n",
    "* 可以更方便的进行数据筛选，如果有序则性能更好;\n",
    "* groupby等操作的结果，如果是多KEY，结果是分层索引，需要会使用;\n",
    "* 一般不需要自己创建分层索引(MultiIndex有构造函数但一般不用)\n",
    "\n",
    "演示数据：百度、阿里巴巴、爱奇艺、京东四家公司的股票数据<br>\n",
    "数据来源：英为财经<br>\n",
    "https://cn.investing.com/\n",
    "\n",
    "---\n",
    "演示提纲:<br>\n",
    "一. Series的分层索引MultiIndex<br>\n",
    "二. Series的分层索引怎么筛选数据？<br>\n",
    "三. DataFrame的多层索引MultiIndex<br>\n",
    "四. DataFrame有多层索引怎样筛选数据？\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks = pd.read_excel('../datas/股票.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12, 8)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>JD</td>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>JD</td>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>JD</td>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>BABA</td>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>BABA</td>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期    公司     收盘     开盘      高      低     交易量     涨跌幅\n",
       "0 2023-07-12    JD  37.41  37.34  37.83  36.91  11.42M  0.0386\n",
       "1 2023-07-11    JD  36.02  35.90  36.39  35.31   6.95M  0.0019\n",
       "2 2023-07-10    JD  35.95  35.30  36.15  35.06   8.33M  0.0053\n",
       "3 2023-07-12  BABA  94.00  94.11  95.03  92.55  23.78M  0.0241\n",
       "4 2023-07-11  BABA  91.79  91.02  92.32  89.01  19.74M  0.0136"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['JD', 'BABA', 'BIDU', 'IQ'], dtype=object)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks[\"公司\"].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=12, step=1)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司\n",
       "BABA     92.116667\n",
       "BIDU    145.036667\n",
       "IQ        5.350000\n",
       "JD       36.460000\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.groupby('公司')[\"收盘\"].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一. Series的分层索引MultiIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BABA  2023-07-10     90.56\n",
       "      2023-07-11     91.79\n",
       "      2023-07-12     94.00\n",
       "BIDU  2023-07-10    142.95\n",
       "      2023-07-11    143.33\n",
       "      2023-07-12    148.83\n",
       "IQ    2023-07-10      5.12\n",
       "      2023-07-11      5.23\n",
       "      2023-07-12      5.70\n",
       "JD    2023-07-10     35.95\n",
       "      2023-07-11     36.02\n",
       "      2023-07-12     37.41\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser = stocks.groupby(['公司','日期'])['收盘'].mean()\n",
    "ser"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "多维索引中，空白的意思是：使用上面的值\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['BABA', 'BIDU', 'IQ', 'JD'], [2023-07-10 00:00:00, 2023-07-11 00:00:00, 2023-07-12 00:00:00]],\n",
       "           labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]],\n",
       "           names=['公司', '日期'])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>日期</th>\n",
       "      <th>2023-07-10 00:00:00</th>\n",
       "      <th>2023-07-11 00:00:00</th>\n",
       "      <th>2023-07-12 00:00:00</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BABA</th>\n",
       "      <td>90.56</td>\n",
       "      <td>91.79</td>\n",
       "      <td>94.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BIDU</th>\n",
       "      <td>142.95</td>\n",
       "      <td>143.33</td>\n",
       "      <td>148.83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IQ</th>\n",
       "      <td>5.12</td>\n",
       "      <td>5.23</td>\n",
       "      <td>5.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>JD</th>\n",
       "      <td>35.95</td>\n",
       "      <td>36.02</td>\n",
       "      <td>37.41</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "日期    2023-07-10  2023-07-11  2023-07-12\n",
       "公司                                      \n",
       "BABA       90.56       91.79       94.00\n",
       "BIDU      142.95      143.33      148.83\n",
       "IQ          5.12        5.23        5.70\n",
       "JD         35.95       36.02       37.41"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# unstack把二级索引变成列\n",
    "ser.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BABA  2023-07-10     90.56\n",
       "      2023-07-11     91.79\n",
       "      2023-07-12     94.00\n",
       "BIDU  2023-07-10    142.95\n",
       "      2023-07-11    143.33\n",
       "      2023-07-12    148.83\n",
       "IQ    2023-07-10      5.12\n",
       "      2023-07-11      5.23\n",
       "      2023-07-12      5.70\n",
       "JD    2023-07-10     35.95\n",
       "      2023-07-11     36.02\n",
       "      2023-07-12     37.41\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th>收盘</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>BABA</td>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>90.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>BABA</td>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>91.79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>BABA</td>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>94.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>BIDU</td>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>142.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BIDU</td>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>143.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>BIDU</td>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>148.83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>IQ</td>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>5.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>IQ</td>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>5.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>IQ</td>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>5.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>JD</td>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>35.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>JD</td>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>36.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>JD</td>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>37.41</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      公司         日期      收盘\n",
       "0   BABA 2023-07-10   90.56\n",
       "1   BABA 2023-07-11   91.79\n",
       "2   BABA 2023-07-12   94.00\n",
       "3   BIDU 2023-07-10  142.95\n",
       "4   BIDU 2023-07-11  143.33\n",
       "5   BIDU 2023-07-12  148.83\n",
       "6     IQ 2023-07-10    5.12\n",
       "7     IQ 2023-07-11    5.23\n",
       "8     IQ 2023-07-12    5.70\n",
       "9     JD 2023-07-10   35.95\n",
       "10    JD 2023-07-11   36.02\n",
       "11    JD 2023-07-12   37.41"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二. Series的分层索引怎么筛选数据？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BABA  2023-07-10     90.56\n",
       "      2023-07-11     91.79\n",
       "      2023-07-12     94.00\n",
       "BIDU  2023-07-10    142.95\n",
       "      2023-07-11    143.33\n",
       "      2023-07-12    148.83\n",
       "IQ    2023-07-10      5.12\n",
       "      2023-07-11      5.23\n",
       "      2023-07-12      5.70\n",
       "JD    2023-07-10     35.95\n",
       "      2023-07-11     36.02\n",
       "      2023-07-12     37.41\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "日期\n",
       "2023-07-10    142.95\n",
       "2023-07-11    143.33\n",
       "2023-07-12    148.83\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.loc['BIDU']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "143.33"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多层索引，可以用元组的形式筛选\n",
    "ser.loc[('BIDU','2023-07-11')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司\n",
       "BABA     91.79\n",
       "BIDU    143.33\n",
       "IQ        5.23\n",
       "JD       36.02\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.loc[:,'2023-07-11']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三. DataFrame的多层索引MultiIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>JD</td>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>JD</td>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>JD</td>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>BABA</td>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>BABA</td>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期    公司     收盘     开盘      高      低     交易量     涨跌幅\n",
       "0 2023-07-12    JD  37.41  37.34  37.83  36.91  11.42M  0.0386\n",
       "1 2023-07-11    JD  36.02  35.90  36.39  35.31   6.95M  0.0019\n",
       "2 2023-07-10    JD  35.95  35.30  36.15  35.06   8.33M  0.0053\n",
       "3 2023-07-12  BABA  94.00  94.11  95.03  92.55  23.78M  0.0241\n",
       "4 2023-07-11  BABA  91.79  91.02  92.32  89.01  19.74M  0.0136"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">JD</th>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">BABA</th>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>90.56</td>\n",
       "      <td>90.05</td>\n",
       "      <td>92.04</td>\n",
       "      <td>89.60</td>\n",
       "      <td>25.19M</td>\n",
       "      <td>0.0001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">BIDU</th>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>148.83</td>\n",
       "      <td>147.44</td>\n",
       "      <td>150.42</td>\n",
       "      <td>145.50</td>\n",
       "      <td>2.41M</td>\n",
       "      <td>0.0384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>143.33</td>\n",
       "      <td>143.23</td>\n",
       "      <td>144.45</td>\n",
       "      <td>140.01</td>\n",
       "      <td>960.45K</td>\n",
       "      <td>0.0027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>142.95</td>\n",
       "      <td>140.72</td>\n",
       "      <td>143.95</td>\n",
       "      <td>140.12</td>\n",
       "      <td>887.82K</td>\n",
       "      <td>0.0020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">IQ</th>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>5.70</td>\n",
       "      <td>5.43</td>\n",
       "      <td>5.78</td>\n",
       "      <td>5.41</td>\n",
       "      <td>19.24M</td>\n",
       "      <td>0.0899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>5.23</td>\n",
       "      <td>5.15</td>\n",
       "      <td>5.30</td>\n",
       "      <td>5.12</td>\n",
       "      <td>5.23M</td>\n",
       "      <td>0.0215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>5.12</td>\n",
       "      <td>5.10</td>\n",
       "      <td>5.19</td>\n",
       "      <td>5.02</td>\n",
       "      <td>8.52M</td>\n",
       "      <td>-0.0078</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低      交易量     涨跌幅\n",
       "公司   日期                                                         \n",
       "JD   2023-07-12   37.41   37.34   37.83   36.91   11.42M  0.0386\n",
       "     2023-07-11   36.02   35.90   36.39   35.31    6.95M  0.0019\n",
       "     2023-07-10   35.95   35.30   36.15   35.06    8.33M  0.0053\n",
       "BABA 2023-07-12   94.00   94.11   95.03   92.55   23.78M  0.0241\n",
       "     2023-07-11   91.79   91.02   92.32   89.01   19.74M  0.0136\n",
       "     2023-07-10   90.56   90.05   92.04   89.60   25.19M  0.0001\n",
       "BIDU 2023-07-12  148.83  147.44  150.42  145.50    2.41M  0.0384\n",
       "     2023-07-11  143.33  143.23  144.45  140.01  960.45K  0.0027\n",
       "     2023-07-10  142.95  140.72  143.95  140.12  887.82K  0.0020\n",
       "IQ   2023-07-12    5.70    5.43    5.78    5.41   19.24M  0.0899\n",
       "     2023-07-11    5.23    5.15    5.30    5.12    5.23M  0.0215\n",
       "     2023-07-10    5.12    5.10    5.19    5.02    8.52M -0.0078"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.set_index(['公司','日期'],inplace=True)\n",
    "stocks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['BABA', 'BIDU', 'IQ', 'JD'], [2023-07-10 00:00:00, 2023-07-11 00:00:00, 2023-07-12 00:00:00]],\n",
       "           labels=[[3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 2, 2], [2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0]],\n",
       "           names=['公司', '日期'])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">BABA</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>90.56</td>\n",
       "      <td>90.05</td>\n",
       "      <td>92.04</td>\n",
       "      <td>89.60</td>\n",
       "      <td>25.19M</td>\n",
       "      <td>0.0001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">BIDU</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>142.95</td>\n",
       "      <td>140.72</td>\n",
       "      <td>143.95</td>\n",
       "      <td>140.12</td>\n",
       "      <td>887.82K</td>\n",
       "      <td>0.0020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>143.33</td>\n",
       "      <td>143.23</td>\n",
       "      <td>144.45</td>\n",
       "      <td>140.01</td>\n",
       "      <td>960.45K</td>\n",
       "      <td>0.0027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>148.83</td>\n",
       "      <td>147.44</td>\n",
       "      <td>150.42</td>\n",
       "      <td>145.50</td>\n",
       "      <td>2.41M</td>\n",
       "      <td>0.0384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">IQ</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>5.12</td>\n",
       "      <td>5.10</td>\n",
       "      <td>5.19</td>\n",
       "      <td>5.02</td>\n",
       "      <td>8.52M</td>\n",
       "      <td>-0.0078</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>5.23</td>\n",
       "      <td>5.15</td>\n",
       "      <td>5.30</td>\n",
       "      <td>5.12</td>\n",
       "      <td>5.23M</td>\n",
       "      <td>0.0215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>5.70</td>\n",
       "      <td>5.43</td>\n",
       "      <td>5.78</td>\n",
       "      <td>5.41</td>\n",
       "      <td>19.24M</td>\n",
       "      <td>0.0899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">JD</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低      交易量     涨跌幅\n",
       "公司   日期                                                         \n",
       "BABA 2023-07-10   90.56   90.05   92.04   89.60   25.19M  0.0001\n",
       "     2023-07-11   91.79   91.02   92.32   89.01   19.74M  0.0136\n",
       "     2023-07-12   94.00   94.11   95.03   92.55   23.78M  0.0241\n",
       "BIDU 2023-07-10  142.95  140.72  143.95  140.12  887.82K  0.0020\n",
       "     2023-07-11  143.33  143.23  144.45  140.01  960.45K  0.0027\n",
       "     2023-07-12  148.83  147.44  150.42  145.50    2.41M  0.0384\n",
       "IQ   2023-07-10    5.12    5.10    5.19    5.02    8.52M -0.0078\n",
       "     2023-07-11    5.23    5.15    5.30    5.12    5.23M  0.0215\n",
       "     2023-07-12    5.70    5.43    5.78    5.41   19.24M  0.0899\n",
       "JD   2023-07-10   35.95   35.30   36.15   35.06    8.33M  0.0053\n",
       "     2023-07-11   36.02   35.90   36.39   35.31    6.95M  0.0019\n",
       "     2023-07-12   37.41   37.34   37.83   36.91   11.42M  0.0386"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.sort_index(inplace=True)\n",
    "stocks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 四. DataFrame有多层索引怎样筛选数据？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**【重要知识】**在选择数据时：\n",
    "* 元组(key1,key2)代表筛选多层索引，其中key1是索引第一级，key2是第二级，比如key1=JD，key2=2023-07-11;\n",
    "* 列表key1，key2代表同一层的多个KEY，其中key1和key2是并列的同级索引，比如key1=JD，key2=BIDU;\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>142.95</td>\n",
       "      <td>140.72</td>\n",
       "      <td>143.95</td>\n",
       "      <td>140.12</td>\n",
       "      <td>887.82K</td>\n",
       "      <td>0.0020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>143.33</td>\n",
       "      <td>143.23</td>\n",
       "      <td>144.45</td>\n",
       "      <td>140.01</td>\n",
       "      <td>960.45K</td>\n",
       "      <td>0.0027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>148.83</td>\n",
       "      <td>147.44</td>\n",
       "      <td>150.42</td>\n",
       "      <td>145.50</td>\n",
       "      <td>2.41M</td>\n",
       "      <td>0.0384</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                收盘      开盘       高       低      交易量     涨跌幅\n",
       "日期                                                         \n",
       "2023-07-10  142.95  140.72  143.95  140.12  887.82K  0.0020\n",
       "2023-07-11  143.33  143.23  144.45  140.01  960.45K  0.0027\n",
       "2023-07-12  148.83  147.44  150.42  145.50    2.41M  0.0384"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc['BIDU']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BIDU</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>142.95</td>\n",
       "      <td>140.72</td>\n",
       "      <td>143.95</td>\n",
       "      <td>140.12</td>\n",
       "      <td>887.82K</td>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低      交易量    涨跌幅\n",
       "公司   日期                                                        \n",
       "BIDU 2023-07-10  142.95  140.72  143.95  140.12  887.82K  0.002"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[('BIDU','2023-07-10'),:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BIDU  2023-07-10    140.72\n",
       "Name: 开盘, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[('BIDU','2023-07-10'),'开盘']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">BIDU</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>142.95</td>\n",
       "      <td>140.72</td>\n",
       "      <td>143.95</td>\n",
       "      <td>140.12</td>\n",
       "      <td>887.82K</td>\n",
       "      <td>0.0020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>143.33</td>\n",
       "      <td>143.23</td>\n",
       "      <td>144.45</td>\n",
       "      <td>140.01</td>\n",
       "      <td>960.45K</td>\n",
       "      <td>0.0027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>148.83</td>\n",
       "      <td>147.44</td>\n",
       "      <td>150.42</td>\n",
       "      <td>145.50</td>\n",
       "      <td>2.41M</td>\n",
       "      <td>0.0384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">JD</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-12</th>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低      交易量     涨跌幅\n",
       "公司   日期                                                         \n",
       "BIDU 2023-07-10  142.95  140.72  143.95  140.12  887.82K  0.0020\n",
       "     2023-07-11  143.33  143.23  144.45  140.01  960.45K  0.0027\n",
       "     2023-07-12  148.83  147.44  150.42  145.50    2.41M  0.0384\n",
       "JD   2023-07-10   35.95   35.30   36.15   35.06    8.33M  0.0053\n",
       "     2023-07-11   36.02   35.90   36.39   35.31    6.95M  0.0019\n",
       "     2023-07-12   37.41   37.34   37.83   36.91   11.42M  0.0386"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[['BIDU','JD'],:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BIDU</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>142.95</td>\n",
       "      <td>140.72</td>\n",
       "      <td>143.95</td>\n",
       "      <td>140.12</td>\n",
       "      <td>887.82K</td>\n",
       "      <td>0.0020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>JD</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低      交易量     涨跌幅\n",
       "公司   日期                                                         \n",
       "BIDU 2023-07-10  142.95  140.72  143.95  140.12  887.82K  0.0020\n",
       "JD   2023-07-10   35.95   35.30   36.15   35.06    8.33M  0.0053"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[(['BIDU','JD'],'2023-07-10'),:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BIDU  2023-07-10    142.95\n",
       "JD    2023-07-10     35.95\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[(['BIDU','JD'],'2023-07-10'),'收盘']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BIDU  2023-07-10    142.95\n",
       "      2023-07-11    143.33\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[('BIDU',['2023-07-10','2023-07-11']),'收盘']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">BABA</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>90.56</td>\n",
       "      <td>90.05</td>\n",
       "      <td>92.04</td>\n",
       "      <td>89.60</td>\n",
       "      <td>25.19M</td>\n",
       "      <td>0.0001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">BIDU</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>142.95</td>\n",
       "      <td>140.72</td>\n",
       "      <td>143.95</td>\n",
       "      <td>140.12</td>\n",
       "      <td>887.82K</td>\n",
       "      <td>0.0020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>143.33</td>\n",
       "      <td>143.23</td>\n",
       "      <td>144.45</td>\n",
       "      <td>140.01</td>\n",
       "      <td>960.45K</td>\n",
       "      <td>0.0027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">IQ</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>5.12</td>\n",
       "      <td>5.10</td>\n",
       "      <td>5.19</td>\n",
       "      <td>5.02</td>\n",
       "      <td>8.52M</td>\n",
       "      <td>-0.0078</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>5.23</td>\n",
       "      <td>5.15</td>\n",
       "      <td>5.30</td>\n",
       "      <td>5.12</td>\n",
       "      <td>5.23M</td>\n",
       "      <td>0.0215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">JD</th>\n",
       "      <th>2023-07-10</th>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-11</th>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低      交易量     涨跌幅\n",
       "公司   日期                                                         \n",
       "BABA 2023-07-10   90.56   90.05   92.04   89.60   25.19M  0.0001\n",
       "     2023-07-11   91.79   91.02   92.32   89.01   19.74M  0.0136\n",
       "BIDU 2023-07-10  142.95  140.72  143.95  140.12  887.82K  0.0020\n",
       "     2023-07-11  143.33  143.23  144.45  140.01  960.45K  0.0027\n",
       "IQ   2023-07-10    5.12    5.10    5.19    5.02    8.52M -0.0078\n",
       "     2023-07-11    5.23    5.15    5.30    5.12    5.23M  0.0215\n",
       "JD   2023-07-10   35.95   35.30   36.15   35.06    8.33M  0.0053\n",
       "     2023-07-11   36.02   35.90   36.39   35.31    6.95M  0.0019"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# slice(None)代表筛选这一索引的所有内容\n",
    "stocks.loc[(slice(None),['2023-07-10','2023-07-11']),:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>BABA</td>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>90.56</td>\n",
       "      <td>90.05</td>\n",
       "      <td>92.04</td>\n",
       "      <td>89.60</td>\n",
       "      <td>25.19M</td>\n",
       "      <td>0.0001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>BABA</td>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>BABA</td>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>BIDU</td>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>142.95</td>\n",
       "      <td>140.72</td>\n",
       "      <td>143.95</td>\n",
       "      <td>140.12</td>\n",
       "      <td>887.82K</td>\n",
       "      <td>0.0020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BIDU</td>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>143.33</td>\n",
       "      <td>143.23</td>\n",
       "      <td>144.45</td>\n",
       "      <td>140.01</td>\n",
       "      <td>960.45K</td>\n",
       "      <td>0.0027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>BIDU</td>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>148.83</td>\n",
       "      <td>147.44</td>\n",
       "      <td>150.42</td>\n",
       "      <td>145.50</td>\n",
       "      <td>2.41M</td>\n",
       "      <td>0.0384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>IQ</td>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>5.12</td>\n",
       "      <td>5.10</td>\n",
       "      <td>5.19</td>\n",
       "      <td>5.02</td>\n",
       "      <td>8.52M</td>\n",
       "      <td>-0.0078</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>IQ</td>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>5.23</td>\n",
       "      <td>5.15</td>\n",
       "      <td>5.30</td>\n",
       "      <td>5.12</td>\n",
       "      <td>5.23M</td>\n",
       "      <td>0.0215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>IQ</td>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>5.70</td>\n",
       "      <td>5.43</td>\n",
       "      <td>5.78</td>\n",
       "      <td>5.41</td>\n",
       "      <td>19.24M</td>\n",
       "      <td>0.0899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>JD</td>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>JD</td>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>JD</td>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      公司         日期      收盘      开盘       高       低      交易量     涨跌幅\n",
       "0   BABA 2023-07-10   90.56   90.05   92.04   89.60   25.19M  0.0001\n",
       "1   BABA 2023-07-11   91.79   91.02   92.32   89.01   19.74M  0.0136\n",
       "2   BABA 2023-07-12   94.00   94.11   95.03   92.55   23.78M  0.0241\n",
       "3   BIDU 2023-07-10  142.95  140.72  143.95  140.12  887.82K  0.0020\n",
       "4   BIDU 2023-07-11  143.33  143.23  144.45  140.01  960.45K  0.0027\n",
       "5   BIDU 2023-07-12  148.83  147.44  150.42  145.50    2.41M  0.0384\n",
       "6     IQ 2023-07-10    5.12    5.10    5.19    5.02    8.52M -0.0078\n",
       "7     IQ 2023-07-11    5.23    5.15    5.30    5.12    5.23M  0.0215\n",
       "8     IQ 2023-07-12    5.70    5.43    5.78    5.41   19.24M  0.0899\n",
       "9     JD 2023-07-10   35.95   35.30   36.15   35.06    8.33M  0.0053\n",
       "10    JD 2023-07-11   36.02   35.90   36.39   35.31    6.95M  0.0019\n",
       "11    JD 2023-07-12   37.41   37.34   37.83   36.91   11.42M  0.0386"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
